package com.pig.controller;

import com.pig.dto.Result;
import com.pig.entity.Order;
import com.pig.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    // 下单（创建订单）
    @PostMapping("/create")
    public Result<Order> createOrder(@RequestBody Order order) {
        try {
            Order created = orderService.createOrder(order);
            return Result.success(created);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 查询当前用户订单列表
    @GetMapping("/my")
    public Result<List<Order>> getMyOrders(@RequestParam Long userId) {
        try {
            List<Order> orders = orderService.getOrdersByUserId(userId);
            return Result.success(orders);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 查询订单详情
    @GetMapping("/detail/{orderId}")
    public Result<Order> getOrderDetail(@PathVariable Long orderId) {
        try {
            Order order = orderService.getOrderDetail(orderId);
            return Result.success(order);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    // 查询所有订单（后台管理用）
    @GetMapping("/list")
    public Result<List<Order>> getAllOrders() {
        try {
            List<Order> orders = orderService.getAllOrders();
            return Result.success(orders);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
} 